<%@ include file="/WEB-INF/tiles/include.jsp"%>

<script type="text/javascript">
	function checkParentMenu(cbObject, strCodeMenu, strViewable)
	{
	    with(document.getElementById("editForm"))
	    {
	    	var menuDeep = strCodeMenu.length;
			var blnView = false;
	
			if(strViewable == 'true') blnView = true;
			//if check
	    	for(var i=1;(i<=((menuDeep/2)-1) && cbObject.checked && blnView);i++){
	        	var parentCode = strCodeMenu.substr(0,(i*2));
	            if (roleMenu.length != null)
	            {
	                for (var j = 0; j < roleMenu.length; j++)
	                {
	                    if(tempCodeName[j].value == parentCode)
	                    {
	                        roleMenu[j].checked = true;
	                    }
	                }
	            }
	    	}
	
	    	//if un-check
	    	if(blnView && !cbObject.checked)
	        {
	            if (roleMenu.length != null)
	            {
	                for (var k = 0; k < roleMenu.length; k++)
	                {
	                    if((tempCodeName[k].value).substr(0,menuDeep) == strCodeMenu)
	                    {
	                        roleMenu[k].checked = false;
	                    }
	                }
	            }
	    	}
	    }
	}
	
	$(document).ready(function($) {
		$("#modalbox_save").click(function() {
			$.wis.util.doSave()
		});
	
		$("#facebox_close").click(function() {
			$.facebox.close()   
		});
	
		$(".roleMenuClass").click(function(){
			var paramStr = $(this).attr("title");
			//alert("paramStr :: "+paramStr);
			var paramMenuCode = paramStr.split(",", 2);
			checkParentMenu(this, paramMenuCode[0], paramMenuCode[1]);
		});
	});
</script>

<form:form id="editForm" commandName="commandRole" method="post" action="${pageContext.request.contextPath}/basic/admin/role.save">
	<form:hidden path="pgIndex"/>
	<form:hidden path="pgRows"/>
	<form:hidden path="pgTotal"/>
	<form:hidden path="id"/>
	<form:hidden path="strAction"/>

<table class="tableInput">
	<tr>
		<th class="field"><spring:message code="label.roleName"/>&nbsp;:</th>
		<td class="even"><form:input path="roleName" size="20" maxlength="20"/></td>
	</tr>
	<tr>
		<th class="field"><spring:message code="label.roleDesc"/>&nbsp;:</th>
		<td class="odd"><form:textarea path="roleDesc" cols="50" rows="5"/></td>
	</tr>
	<tr>
		<th class="field"><spring:message code="label.roleOrder"/>&nbsp;:</th>
		<td class="even">
			<form:select path="roleOrder">
				<form:option value=""></form:option>
				<form:option value="1">1</form:option>
				<form:option value="2">2</form:option>
				<form:option value="3">3</form:option>
				<form:option value="4">4</form:option>
				<form:option value="5">5</form:option>
			</form:select>
		</td>
	</tr>		
	<tr>
		<th class="field">
			<span class="styled"><spring:message code="label.menuAuthorized"/>&nbsp;:</span>
		</th>
		<td class="odd">
			<table class="tableInput">
				<c:forEach 
					items="${commandRole.menuCollection}" 
					var="menuItem" varStatus="menuSt">
					<tr>
						<td align="left" style="border-bottom: 1px solid #517DBF;">
							<input type="hidden" name="tempCodeName" id="tempCodeName" value="${menuItem.menuCode}">
							<form:checkbox cssClass="roleMenuClass"
								path="roleMenu" 
								value="${menuItem.id}" 
								title="${menuItem.menuCode},${menuItem.menuViewable}" />
							&nbsp;<spring:message code="${menuItem.menuName}" />
							<c:if test="${menuItem.menuViewable}">
								&nbsp;-&nbsp;[<span style="color: green; font-style: italic">
								<spring:message code="label.viewable" /> </span>]
							</c:if>
						</td>
					</tr>
				</c:forEach>
			</table>
		</td>
	</tr>
</table>
<table class="tableButton">
	<tr><td align="left">
		<span class="button"><a href="#modalbox_save" id="modalbox_save"><spring:message code="button.save"/></a></span>
		<span class="button"><a href="#facebox_close" id="facebox_close"><spring:message code="button.cancel"/></a></span>
	</td></tr>
</table>
</form:form>